﻿namespace CardGameCore.Battle
{
    public class PlayerSelectCard : IPlayerSelectCard
    {
        private readonly IBattlefield _battlefield;
        private readonly ISelectCardJudge _selectCardJudge;
        private readonly PlayerSelectCardResult _playerSelectCardResult = new PlayerSelectCardResult();
        
        public PlayerSelectCard(IBattlefield battlefield, ISelectCardJudge selectCardJudge)
        {
            _battlefield = battlefield;
            _selectCardJudge = selectCardJudge;
        }

        public PlayerSelectCardResult Process(SelectCardParameters parameters)
        {
            if (_battlefield.Player.HandCards.Count <= parameters.Index)
            {
                _playerSelectCardResult.Success = false;
                return _playerSelectCardResult;
            }

            var card = _battlefield.Player.HandCards[parameters.Index];
            if (_selectCardJudge.TryAddBattleCard(_battlefield.Player, card, parameters.Position))
            {
                _playerSelectCardResult.Success = true;
                _battlefield.Player.BattleCards.Add(parameters.Position, card);
                _battlefield.Player.HandCards.Remove(card);
                card.Position = parameters.Position;
                _battlefield.Player.BattleCost += card.Cost;
            }

            return _playerSelectCardResult;
        }
    }
}